<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $iduser
 * @property string $username
 * @property string $password
 * @property string $batch
 * @property string $fullname
 * @property integer $team
 * @property string $enabled
 * @property integer $idrole
 * @property string $restricted
 * @property string $email
 *
 * The followings are the available model relations:
 * @property ActivityLog[] $activityLogs
 * @property Callback[] $callbacks
 * @property Callback[] $callbacks1
 * @property Notifications[] $notifications
 * @property QshiftChange[] $qshiftChanges
 * @property QshiftChange[] $qshiftChanges1
 * @property ShiftChange[] $shiftChanges
 * @property ShiftChange[] $shiftChanges1
 * @property Role $idrole0
 * @property Team $team0
 * @property Schedule[] $schedules
 */
class User extends CActiveRecord
{

	public $confirmPassword;
	
	public static $yesNoValues= array('0'=>'No', '1'=>'Yes');

	 public static function getYesNoValue($bin=null){
	 
		if($bin!==null)
			return self::$yesNoValues[$bin];
		return self::$yesNoValues;
	 }


	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, batch, fullname, team, enabled, idrole, email', 'required'),
			array('iduser, team, idrole', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>10),
			array('password, fullname, email', 'length', 'max'=>45),
			array('batch', 'length', 'max'=>5),
			//array('confirmPassword', 'validatePassword'),			
			array('enabled, restricted', 'length', 'max'=>1),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('iduser, username, password, batch, fullname, team, enabled, idrole, email, restricted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'activityLogs' => array(self::HAS_MANY, 'ActivityLog', 'username'),
			'callbacks' => array(self::HAS_MANY, 'Callback', 'created_by'),
			'callbacks1' => array(self::HAS_MANY, 'Callback', 'new_owner'),
			'notifications' => array(self::HAS_MANY, 'Notifications', 'username'),
			'qshiftChanges' => array(self::HAS_MANY, 'QshiftChange', 'requesting_user'),
			'qshiftChanges1' => array(self::HAS_MANY, 'QshiftChange', 'target_user'),
			'shiftChanges' => array(self::HAS_MANY, 'ShiftChange', 'requesting_user'),
			'shiftChanges1' => array(self::HAS_MANY, 'ShiftChange', 'target_user'),
			'role_relation' => array(self::BELONGS_TO, 'Role', 'idrole'),
			'team_relation' => array(self::BELONGS_TO, 'Team', 'team'),
			'schedules' => array(self::MANY_MANY, 'Schedule', 'user_schedule(iduser, idschedule)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'iduser' => 'Id',
			'username' => 'Username',
			'password' => 'Password',			
			'batch' => 'Batch Id',
			'fullname' => 'Full Name',
			'team' => 'Team',
			'enabled' => 'Enabled',
			'idrole' => 'Role ID',
			'email' => 'Email',
			'restricted' => 'Restricted',
		);
	}
	

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($team_id=null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		if($team_id != null)
			$criteria->condition = 'team = '.$team_id. ' and restricted = 1';		
		
		$criteria->compare('iduser',$this->iduser);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('batch',$this->batch,true);
		$criteria->compare('fullname',$this->fullname,true);
		$criteria->compare('team',$this->team);
		$criteria->compare('enabled',$this->enabled,true);
		$criteria->compare('idrole',$this->idrole);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('restricted',$this->restricted,true);
		$criteria->order = 'username ASC';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	
}